package com.example.banksys;

import DataBase.myDB;
import com.pixelduke.control.skin.FXSkins;
import handler.queryHandler;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundSize;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.sql.SQLException;

public class ManageTradeManageController {
    public TableView<tradeItem> tradeTable;
    public TableColumn<tradeItem,Number> tradeID;//
    public TableColumn<tradeItem,String> tradeName;//
    public TableColumn<tradeItem,String> tradeType;//
    public TableColumn<tradeItem,Boolean> descriptionButton;
    public TableColumn<tradeItem,String> tradeDuration;//
    public TableColumn<tradeItem,Boolean> delete;
    public ComboBox<String> typeChoose;
    public Button addNew;

    public static SimpleBooleanProperty needRefresh=new SimpleBooleanProperty(false);


    ObservableList<tradeItem> tradeItemObservableList;


    public void initialize() throws SQLException {
        tradeItemObservableList= FXCollections.observableArrayList();
        typeChoose.getItems().add("all");
        typeChoose.getItems().add("save");
        typeChoose.getItems().add("take");
        typeChoose.getItems().add("loan");
        typeChoose.getItems().add("financialProduct");
        queryHandler qHandler=new queryHandler();
        qHandler.tradeItemsQuery(typeChoose,tradeItemObservableList);
        tradeTable.setItems(tradeItemObservableList);
        tradeID.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<tradeItem, Number> tradeItemNumberCellDataFeatures) {
                return tradeItemNumberCellDataFeatures.getValue().getTradeID();
            }
        });
        tradeType.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<tradeItem, String> tradeItemStringCellDataFeatures) {
                return tradeItemStringCellDataFeatures.getValue().getTradeType();
            }
        });
        tradeName.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<tradeItem, String> tradeItemStringCellDataFeatures) {
                return tradeItemStringCellDataFeatures.getValue().getTradeName();
            }
        });
        tradeDuration.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<tradeItem, String> tradeItemStringCellDataFeatures) {
                return tradeItemStringCellDataFeatures.getValue().getTradeDuration();
            }
        });
        descriptionButton.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<tradeItem, Boolean> tradeItemBooleanCellDataFeatures) {
                return new SimpleBooleanProperty(tradeItemBooleanCellDataFeatures.getValue() != null);
            }
        });
        delete.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeItem, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<tradeItem, Boolean> tradeItemBooleanCellDataFeatures) {
                return new SimpleBooleanProperty(tradeItemBooleanCellDataFeatures.getValue() != null);
            }
        });
        descriptionButton.setCellFactory(new Callback<TableColumn<tradeItem, Boolean>, TableCell<tradeItem, Boolean>>() {
            @Override
            public TableCell<tradeItem, Boolean> call(TableColumn<tradeItem, Boolean> tradeItemBooleanTableColumn) {
                return new TableCell<>(){
                    @Override
                    protected void updateItem(Boolean Item, boolean notempty) {
                        super.updateItem(Item,notempty);
                        Button button=new Button();
                        button.setText("详情");
                        button.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent actionEvent) {
                                FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("description.fxml"));
                                Scene scene = null;
                                try {
                                    scene = new Scene(fxmlLoader.load());
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                }
                                //scene.getStylesheets().add(FXSkins.getStylesheetURL());
                                DescriptionController desctrl=fxmlLoader.getController();
                                desctrl.description.setText(tradeItemObservableList.get(getIndex()).getTradeDescription().getValue());
                                Stage t=new Stage();
                                t.setScene(scene);
                                t.show();
                            }
                        });
                        if (notempty) {
                            //如果此列为空默认不添加元素
                            setText(null);
                            setGraphic(null);
                        } else {
                            this.setGraphic(button);
                        }
                    }
                };
            }
        });

        delete.setCellFactory(new Callback<TableColumn<tradeItem, Boolean>, TableCell<tradeItem, Boolean>>() {
            @Override
            public TableCell<tradeItem, Boolean> call(TableColumn<tradeItem, Boolean> tradeItemBooleanTableColumn) {
                return new TableCell<>(){
                    @Override
                    protected void updateItem(Boolean aBoolean, boolean b) {
                        super.updateItem(aBoolean, b);
                        Button button=new Button();
                        button.setText("删除");
                        button.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent actionEvent) {
                                myDB database=new myDB("banksystem");
                                String sql="delete from trade_type where  type_ID ="+tradeItemObservableList.get(getIndex()).getTradeID().getValue();
                                try {
                                    database.getStatement().execute(sql);
                                } catch (SQLException e) {
                                    throw new RuntimeException(e);
                                }
                                tradeItemObservableList.remove(getIndex());
                                tradeTable.refresh();
                                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                                alert.titleProperty().set("信息");
                                alert.headerTextProperty().set("删除成功！");
                                alert.showAndWait();
                            }
                        });
                        if (b) {
                            //如果此列为空默认不添加元素
                            setText(null);
                            setGraphic(null);
                        } else {
                            this.setGraphic(button);
                        }
                    }
                };
            }
        });

        typeChoose.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                tradeTable.getItems().remove(0,tradeTable.getItems().size());
                try {
                    new queryHandler().tradeItemsQuery(typeChoose,tradeItemObservableList);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                tradeTable.setItems(tradeItemObservableList);
                tradeTable.refresh();
            }
        });

        needRefresh.addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean old, Boolean changed) {
                if(!old&&changed){
                    tradeTable.getItems().remove(0,tradeTable.getItems().size());
                    try {
                        new queryHandler().tradeItemsQuery(typeChoose,tradeItemObservableList);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    tradeTable.setItems(tradeItemObservableList);
                    tradeTable.refresh();
                    needRefresh.set(false);
                }
            }
        });
    }

    public void addNewType(){
        FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("addTradeItem.fxml"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //scene.getStylesheets().add(FXSkins.getStylesheetURL());
        Stage t=new Stage();
        t.setScene(scene);
        t.setResizable(false);
        t.show();
    }
}
